package net.kts.story.menu;

import java.io.File;
import java.net.URL;

import net.kts.help.HelpLayout;
import net.kts.help.HelpPreferences;
import net.kts.help.HelpService;
import net.kts.main.R;
import net.kts.story.StoryObjectHouse;
import net.kts.story.progress.StoryProgressDialog;
import net.kts.transrate.TranslateService;
import twitter4j.TwitterException;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class StoryMenu {
	private Activity activity;
	private View menu;
	private Handler handler;
	private View messageView;
	private View memoriesMenuView;
	private File attachImageFile;
	private Animation clickAnimation;
	private StoryObjectHouse storyObjectHouse;
	
	public StoryMenu(StoryObjectHouse storyObjectHouse) {
		this.activity = storyObjectHouse.getMainActivity();
		this.handler = new Handler();
		this.storyObjectHouse = storyObjectHouse;
		clickAnimation = AnimationUtils.loadAnimation(activity, R.anim.button_click);
	}

	
	public void addMenuContentView() {
		LayoutInflater inflater = activity.getLayoutInflater();
        menu = inflater.inflate(R.layout.story_menu, null);
        activity.addContentView(menu, new LinearLayout.LayoutParams(
        		LayoutParams.FILL_PARENT, 
        		LayoutParams.FILL_PARENT) 
        );
        Button refresh = (Button) menu.findViewById(R.id.story_menu_refresh);
        Button memories = (Button) menu.findViewById(R.id.story_menu_memories);
        refresh.setOnClickListener(refreshClickListener);
        memories.setOnClickListener(memoriesMenuClickListener);
	}
	
	public void addMemoriesMenuContentView() {
		LayoutInflater inflater = activity.getLayoutInflater();
        memoriesMenuView = inflater.inflate(R.layout.story_memories, null);
        memoriesMenuView.setVisibility(View.INVISIBLE);
        
        Button memories = (Button) memoriesMenuView.findViewById(R.id.story_memories_memories);
		memories.setOnClickListener(memoriesClickListener);
		
		Button cancelBtn = (Button) memoriesMenuView.findViewById(R.id.story_memories_cancel);
		cancelBtn.setOnClickListener(cancelClickListener);
		
		Button galleryBtn = (Button) memoriesMenuView.findViewById(R.id.story_memories_gallery);
		galleryBtn.setOnClickListener(galleryClickListener);
		
		
		activity.addContentView(memoriesMenuView, new LinearLayout.LayoutParams(
        		LayoutParams.FILL_PARENT, 
        		LayoutParams.WRAP_CONTENT) 
        );
		
	}
	
	public void addAttachTweetImage(Uri uri){
		ImageView imageView = (ImageView) memoriesMenuView.findViewById(R.id.story_memories_image);
		imageView.setVisibility(View.VISIBLE);
		imageView.setImageURI(uri);
		
		String[] projection = { MediaColumns.DATA };
		Cursor cur = activity.managedQuery(uri, projection, null, null, null);
		cur.moveToFirst();
		String path = cur.getString(cur.getColumnIndex(MediaColumns.DATA));
		
		attachImageFile = new File(path);
	}
	
	
	public void addMessageContentView() {
		LayoutInflater inflater = activity.getLayoutInflater();
        messageView = inflater.inflate(R.layout.story_message, null);
        messageView.setVisibility(View.INVISIBLE);
        
        activity.addContentView(messageView, new LinearLayout.LayoutParams(
        		LayoutParams.FILL_PARENT, 
        		LayoutParams.WRAP_CONTENT) 
        );
	}	
	public void showMessageView(String title, String content, URL imageURL){	
		StoryProgressDialog.showProgressDiaglog();
		TextView titleView = (TextView) this.messageView.findViewById(R.id.story_message_title);
		TextView contentView = (TextView) this.messageView.findViewById(R.id.story_message_content);
		ImageView imageView = (ImageView) this.messageView.findViewById(R.id.story_message_image);
		
		imageView.setImageBitmap(null);
		imageView.setVisibility(View.INVISIBLE);
		
		titleView.setText(title);
		contentView.setText(content);
		
		this.messageView.setVisibility(View.VISIBLE);
		this.messageView.startAnimation(AnimationUtils.loadAnimation(memoriesMenuView.getContext(), R.anim.storywrite));
		
		if(imageURL != null){
			imageView.setVisibility(View.VISIBLE);
			Thread worker = new Thread(new ImageDownloadTask(imageURL, imageView));
			worker.start();
		}
		if(imageURL == null)
			StoryProgressDialog.dismissProgressDiaglog();
	}
	
	
	public void dismissMessageView(){
		if(messageView.getVisibility() == View.VISIBLE){
			TextView titleView = (TextView) this.messageView.findViewById(R.id.story_message_title);
			TextView contentView = (TextView) this.messageView.findViewById(R.id.story_message_content);
			ImageView imageView = (ImageView) this.messageView.findViewById(R.id.story_message_image);
			titleView.setText("");
			contentView.setText("");
			imageView.setImageBitmap(null);
			imageView.setVisibility(View.INVISIBLE);
			this.messageView.setAnimation(AnimationUtils.loadAnimation(memoriesMenuView.getContext(), R.anim.storywrite_out));	
			this.messageView.setVisibility(View.INVISIBLE);
		}
	}
	
	final OnClickListener refreshClickListener= new OnClickListener(){
		@Override
		public void onClick(View v) {
			v.startAnimation(clickAnimation);
			StoryProgressDialog.showProgressDiaglog();
			Thread worker = new Thread(new Runnable() {
				@Override
				public void run() {
					handler.post(new Runnable() {
						@Override
						public void run() {
							storyObjectHouse.surroundTwitter();
						}
					});
				}
			});
			worker.start();
			StoryProgressDialog.dismissProgressDiaglog();
		} 
	};
	
	final OnClickListener memoriesMenuClickListener= new OnClickListener(){
		@Override
		public void onClick(View v) {
			v.startAnimation(clickAnimation);
			memoriesMenuView.setVisibility(View.VISIBLE);
			memoriesMenuView.setAnimation(AnimationUtils.loadAnimation(memoriesMenuView.getContext(), R.anim.storywrite));
	        EditText editText = (EditText) memoriesMenuView.findViewById(R.id.twitterEditText);
	        editText.setText("");
	        if(attachImageFile == null)
	        	memoriesMenuView.findViewById(R.id.story_memories_image).setVisibility(v.INVISIBLE);
	        
	        dismissMessageView();
	        
	        	
	        
	        
	        if(HelpPreferences.isShowing())
		        new HelpService(activity)
		        		.add(new HelpLayout(R.drawable.help_story_memories));
		} 
	};

	
	final OnClickListener galleryClickListener= new OnClickListener(){
		@Override
		public void onClick(View v) {
			v.startAnimation(clickAnimation);
			Intent i = new Intent(Intent.ACTION_PICK);
			i.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
			i.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			activity.startActivityForResult(i, 2);
		}
	};
	final OnClickListener memoriesClickListener= new OnClickListener(){
		@Override
		public void onClick(final View v) {
			v.startAnimation(clickAnimation);
			EditText editText = (EditText) memoriesMenuView.findViewById(R.id.twitterEditText);
			final String text = editText.getText().toString();
	
			if(text.trim().equals("")){
				Toast.makeText(activity.getApplicationContext(), TranslateService.translateRepository("글자를 입력하세요"),1).show();
				return;
			}
			if(text.length() > 128){
				Toast.makeText(activity.getApplicationContext(), TranslateService.translateRepository("128자를 초과 하였습니다"), 1).show();
				return;
			}
			
			StoryProgressDialog.showProgressDiaglog();
			
			Thread worker = new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						boolean success = false;
						if(attachImageFile != null){
							success = storyObjectHouse.getTwitterConator().submitImageByGeoLocation(text, attachImageFile,
																					storyObjectHouse.getSelectedLocation());
						}
						if(attachImageFile == null)					
							success = storyObjectHouse.getTwitterConator().submitByGeoLocation(text, storyObjectHouse.getSelectedLocation());
						Log.i("kts","Story Memories regist : "+success);
						if(success){
							handler.postDelayed(new Runnable() {
								@Override
								public void run() {
									attachImageFile = null;	
									Toast.makeText(activity.getApplicationContext(), TranslateService.translateRepository("등록 하였습니다."), 1).show();
									memoriesMenuView.setVisibility(View.INVISIBLE);
									storyObjectHouse.surroundTwitterAndMyTweet(storyObjectHouse.getSelectedLocation());
									cancelClickListener.onClick(v);
								}
							},1000);
						}
						
					} catch (TwitterException e) {
						handler.post(new Runnable() {
							@Override
							public void run() {
								Toast.makeText(activity.getApplicationContext(), TranslateService.translateRepository("등록 실패 하였습니다"), 1).show();
							}
						});
						Log.e("kts", "등록 실패 하였습니다 \r\n"+ e.getMessage());
					} finally{
						StoryProgressDialog.dismissProgressDiaglog();
					}
				}
			});
			
			worker.start();
		}
	};
	
	final OnClickListener cancelClickListener= new OnClickListener(){
		@Override
		public void onClick(View v) {
			v.startAnimation(clickAnimation);
			memoriesMenuView.setAnimation(AnimationUtils.loadAnimation(memoriesMenuView.getContext(), R.anim.storywrite_out));
			memoriesMenuView.setVisibility(View.INVISIBLE);
			attachImageFile = null;
		} 
	};
}
